﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace WISBenchmarkClient
{

    class Deck
    {
        private readonly List<String> _cards = new List<string>();
        public readonly List<String> Query = new List<string>();
        private Haufigkeit _haufigkeit = new Haufigkeit();

        /// <summary>
        /// get and remove the top card from the Deck
        /// </summary>
        /// <returns>top card's link</returns>
        public String GetCard()
        {
            if (_cards.Count == 0)
            {
                FillDeck();
            }
            var topCard = _cards.ElementAt(_cards.Count - 1 );
            _cards.RemoveAt(_cards.Count - 1);
            return topCard;
        }

        /// <summary>
        /// Fill the deck
        /// </summary>
        private void FillDeck()
        {
            for (var i = 0; i < 6; i++)
            {
                for (var j = 0; j < _haufigkeit.Q[i]; j++)
                {
                    _cards.Add(Query.ElementAt(i));
                } 
            }

            Shuffle();
        }

        /// <summary>
        /// Shuffle the cards in the Deck
        /// </summary>
        private void Shuffle()
        {
            var rnd = new Random();
            for (var i = _cards.Count; i > 1; i--)
            {
                var pos = rnd.Next(i);
                var x = _cards[i - 1];
                _cards[i - 1] = _cards[pos];
                _cards[pos] = x;
            }
        }

        /// <summary>
        /// Initialize the deck
        /// </summary>
        public void NewDeck(Haufigkeit haufigkeit)
        {
            _haufigkeit = haufigkeit;
            FillDeck();
        }
    }
}
